﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net;

namespace Gosocket.SDK
{
    public class ApiManager: WebManager
    {   
        public ApiManager(Guid ApplicationId, string Token)
        {
            _ApplicationId = ApplicationId;
            _Secret = Token;
            
            CreateWebClient();
        }

        public ApiManager(String urlservice, Guid ApplicationId, string Token)
        {
            _default_url_service = urlservice;
            
            _ApplicationId = ApplicationId;
            _Secret = Token;
            
            CreateWebClient();
        }

		public ApiManager(Guid ApplicationId, string Token, string proxy, int port, string proxy_user, string proxy_password)
			: this(_default_url_service, proxy, port, proxy_user, proxy_password, ApplicationId, Token)
		{
		}

        public ApiManager(String urlservice, String proxy, int port, String proxy_user, String proxy_password, Guid ApplicationId, string Token)            
        {
            _default_url_service = urlservice;
            _proxy = proxy;
            _port = port;
            _proxy_user = proxy_user;
            _proxy_password = proxy_password;

            _ApplicationId = ApplicationId;
            _Secret = Token;

            CreateWebClient();
        }        

        /// <summary>
        /// Gets the user based on the security credentials used
        /// </summary>
        /// <returns>Domain.User</returns>
        public Domain.User GetUser()
        {
            HttpResponseMessage resp = httpClient.GetAsync("api/Gadget/GetUser").Result;
            resp.EnsureSuccessStatusCode();

            Domain.User result = resp.Content.ReadAsAsync<Domain.User>().Result;

            resp.Dispose();

            return result;
        }

        /// <summary>
        /// Gets a list of accounts
        /// </summary>
        /// <returns>Domain.Account</returns>
        public IEnumerable<Domain.Account> GetMyAccounts()
        {
			//httpClient.BaseAddress = new Uri("http://www.filehippo.com");
			//httpClient.DefaultRequestHeaders.Authorization = null;
			//HttpResponseMessage resp = httpClient.GetAsync("").Result;
			HttpResponseMessage resp = httpClient.GetAsync("api/Gadget/GetMyAccounts").Result;
			resp.EnsureSuccessStatusCode();

            IEnumerable<Domain.Account> result = resp.Content.ReadAsAsync<IEnumerable<Domain.Account>>().Result;

            resp.Dispose();

            return result;
        }

        /// <summary>
        /// /// Gets a list of clients
        /// </summary>
        /// <param name="MyAccountId">Account Id</param>
        /// <param name="Page">Page number</param>
        /// <returns>IEnumerable of Domain.Account</returns>
        public IEnumerable<Domain.Account> GetClients(Guid AccountId, int Page = 1)
        {
            string requestUri = string.Format("api/Gadget/GetClients/?MyAccountId={0}&Page={1}", AccountId, Page);

            HttpResponseMessage resp = httpClient.GetAsync(requestUri).Result;
            resp.EnsureSuccessStatusCode();

            IEnumerable<Domain.Account> result = resp.Content.ReadAsAsync<IEnumerable<Domain.Account>>().Result;

            resp.Dispose();

            return result;
        }

        /// <summary>
        /// Gets a list of vendors
        /// </summary>
        /// <param name="MyAccountId">Account Id</param>
        /// <param name="Page">Page number</param>
        /// <returns>IEnumerable of Domain.Account</returns>
        public IEnumerable<Domain.Account> GetVendors(Guid AccountId, int Page = 1)
        {
            string requestUri = string.Format("api/Gadget/GetVendors/?MyAccountId={0}&Page={1}", AccountId, Page);

            HttpResponseMessage resp = httpClient.GetAsync(requestUri).Result;
            resp.EnsureSuccessStatusCode();

            IEnumerable<Domain.Account> result = resp.Content.ReadAsAsync<IEnumerable<Domain.Account>>().Result;

            resp.Dispose();

            return result;
        }

        /// <summary>
        /// List of document types
        /// </summary>
        /// <returns>IEnumerable of Domain.DocumentType</returns>
        public IEnumerable<Domain.DocumentType> GetDocumentTypes()
        {
            HttpResponseMessage resp = httpClient.GetAsync("api/Gadget/GetDocumentTypes").Result;
            resp.EnsureSuccessStatusCode();

            IEnumerable<Domain.DocumentType> result = resp.Content.ReadAsAsync<IEnumerable<Domain.DocumentType>>().Result;

            resp.Dispose();

            return result;
        }

        /// <summary>
       /// A list of sent documents
       /// </summary>
       /// <param name="MyAccountId">Account id</param>
       /// <param name="FromDate">From date</param>
       /// <param name="ToDate">To date</param>
       /// <param name="DocumentTypeId">Document type id</param>
       /// <param name="ReceiverCode">Receiver code</param>
       /// <param name="Number">Document number</param>
       /// <param name="Page">Page number</param>
       /// <param name="ReadMode">Read mode</param>
       /// <returns></returns>
        public IEnumerable<Domain.Document> GetSentDocuments(Guid AccountId, DateTime FromDate, DateTime ToDate, int DocumentTypeId, string ReceiverCode, long Number, int Page = 1, Domain.ReadMode ReadMode = Domain.ReadMode.All)
        {
            string requestUri = string.Format("api/Gadget/GetSentDocuments/?MyAccountId={0}&FromDate={1}&ToDate={2}&DocumentTypeId={3}&SenderCode={4}&Number={5}&Page={6}&ReadMode={7}",
                AccountId,
                FromDate.ToString("MM-dd-yyyy"),
                ToDate.ToString("MM-dd-yyyy"),
                DocumentTypeId,
                ReceiverCode,
                Number,
                Page,
                ReadMode);

            HttpResponseMessage resp = httpClient.GetAsync(requestUri).Result;
            resp.EnsureSuccessStatusCode();

            IEnumerable<Domain.Document> result = resp.Content.ReadAsAsync<IEnumerable<Domain.Document>>().Result;

            resp.Dispose();

            return result;
        }

        /// <summary>
        /// A list of sent documents
        /// </summary>
        /// <param name="MyAccountId">Account id</param>
        /// <param name="FromDate">From date</param>
        /// <param name="ToDate">To date</param>
        /// <param name="DocumentTypeId">Document type id</param>
        /// <param name="ReceiverCode">Receiver code</param>
        /// <param name="Number">Document number</param>
        /// <param name="Page">Page number</param>
        /// <param name="ReadMode">Read mode</param>
        /// <returns></returns>
        public IEnumerable<Domain.Document> GetReceivedDocuments(Guid AccountId, DateTime FromDate, DateTime ToDate, int DocumentTypeId, string SenderCode, long Number, int Page = 1, Domain.ReadMode ReadMode = Domain.ReadMode.All)
        {
            string requestUri = string.Format("api/Gadget/GetReceivedDocuments/?MyAccountId={0}&FromDate={1}&ToDate={2}&DocumentTypeId={3}&SenderCode={4}&Number={5}&Page={6}&ReadMode={7}",
                AccountId,
                FromDate.ToString("MM-dd-yyyy"),
                ToDate.ToString("MM-dd-yyyy"),
                DocumentTypeId,
                SenderCode,
                Number,
                Page,
                ReadMode);

            HttpResponseMessage resp = httpClient.GetAsync(requestUri).Result;
            resp.EnsureSuccessStatusCode();

            IEnumerable<Domain.Document> result = resp.Content.ReadAsAsync<IEnumerable<Domain.Document>>().Result;

            resp.Dispose();

            return result;
        }

        /// <summary>
        /// List of 
        /// </summary>
        /// <param name="MyAccountId">Account Id</param>
        /// <param name="PO"></param>
        /// <param name="Page">Page number</param>
        /// <returns></returns>
        public IEnumerable<Domain.Document> GetSentDocumentsByPO(Guid AccountId, string PO, int Page = 1)
        {
            string requestUri = string.Format("api/Gadget/GetSentDocumentsByPO/?MyAccountId={0}&PO={1}&Page={2}", AccountId, PO, Page);
            HttpResponseMessage resp = httpClient.GetAsync(requestUri).Result;
            resp.EnsureSuccessStatusCode();

            IEnumerable<Domain.Document> result = resp.Content.ReadAsAsync<IEnumerable<Domain.Document>>().Result;

            resp.Dispose();

            return result;
        }

        /// <summary>
        /// Get Xml File
        /// </summary>
        /// <param name="DocumentId">Document Id</param>
        /// <returns>Document byte[]</returns>
        public byte[] GetXml(Guid DocumentId)
        {
            string requestUri = string.Format("api/Gadget/GetXml/?DocumentId={0}", DocumentId);
            HttpResponseMessage resp = httpClient.GetAsync(requestUri).Result;
            resp.EnsureSuccessStatusCode();

            byte[] result = resp.Content.ReadAsAsync<byte[]>().Result;

            resp.Dispose();

            return result;
        }

        /// <summary>
        /// Get Distribution Rule
        /// </summary>
        /// <param name="AccountId">Account Id</param>
        /// <returns>IEnumerable<Domain.DistributionRule></returns>
        public IEnumerable<Domain.DistributionRule> GetDistributionRules(Guid AccountId)
        {
            string requestUri = string.Format("api/Gadget/GetDistributionRules/?AccountId={0}", AccountId);
            HttpResponseMessage resp = httpClient.GetAsync(requestUri).Result;
            resp.EnsureSuccessStatusCode();

            IEnumerable<Domain.DistributionRule> result = resp.Content.ReadAsAsync<IEnumerable<Domain.DistributionRule>>().Result;

            resp.Dispose();

            return result;
        }        
    }
}
